package com.tedu.healthcounse.service.impl;

import com.tedu.healthcounse.mapper.HealthCounseMapper;
import com.tedu.healthcounse.pojo.dto.HealthCounseParam;
import com.tedu.healthcounse.pojo.dto.HealthCounseQuery;
import com.tedu.healthcounse.pojo.entity.HealthCounse;
import com.tedu.healthcounse.pojo.vo.HealthCounseVO;
import com.tedu.healthcounse.service.HealthCounseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class HealthCounseServiceImpl implements HealthCounseService {

    @Autowired
    private HealthCounseMapper healthCounseMapper;

    @Override
    public List<HealthCounseVO> select(HealthCounseQuery healthCounseQuery) {
        log.debug("查询文章列表,参数:{}", healthCounseQuery);
        List<HealthCounseVO> list = healthCounseMapper.select(healthCounseQuery);
        return list;
    }

    @Override
    public void delete(Integer id) {
        log.debug("删除文章业务，参数={}", id);
        healthCounseMapper.delete(id);
    }

    @Override
    public void save(HealthCounseParam healthCounseParam) {
        log.debug("保存文章业务，参数={}", healthCounseParam);
        HealthCounse healthCounse = new HealthCounse();
        BeanUtils.copyProperties(healthCounseParam, healthCounse);
        if (healthCounseParam.getId() == null){
            healthCounse.setCreateTime(new Date());
            healthCounse.setUpdateTime(new Date());
            healthCounseMapper.insert(healthCounse);
        }else {
            healthCounse.setUpdateTime(new Date());
            healthCounseMapper.update(healthCounse);
        }
    }

}
